// pages/upload/upload.js

import siteinfo from '../../siteinfo.js';
const apiroot = siteinfo.siteroot + 'index.php?s=/api/';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uploadImgData: [],
    region: ['山东省', '济南市', '历城区'],
    address: '',
    longitude: '',
    latitude: '',
    lianxiren1: '',
    dianhua1: '',
    shijian1: '',
    mendian1: '',
    jieshao1: '',
    login_id: '',
    dataList: [],
  
   
   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.request({
      url: apiroot +'manba/acavtlist', //仅为示例，并非真实的接口地址
      data: {
        latitude: wx.getStorageSync('location').latitude,
        longitude: wx.getStorageSync('location').longitude
      },

      success(res) {
        that.setData({
          dataList: res.data.result
        })
        console.log(res.data)
      }
    })










    if (!wx.getStorageSync('token')) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
    wx.removeStorageSync('logo_image_id')
  },
  changeSearch(e) {
    var that = this
    wx.request({
      url: apiroot +'manba/searchlist', //仅为示例，并非真实的接口地址
      data: {
        name: e.detail.value,
        latitude: wx.getStorageSync('location').latitude,
        longitude: wx.getStorageSync('location').longitude
      },

      success(res) {
        that.setData({
          dataList: res.data.result
        })
        console.log(res.data)
      }
    })
    this.setData({
      lianxiren1: e.detail.value
    })
  },
  sousuo() {
    var that = this
    console.log(that.data.lianxiren1)


    if (that.data.lianxiren1 == '') {
      wx.showToast({
        title: '请输入门店',
        icon: 'error',
        duration: 2000
      })
      return false;
    }
    wx.request({
      url: apiroot + 'manba/searchlist', //仅为示例，并非真实的接口地址
      data: {
        name: this.data.lianxiren1,
        latitude: wx.getStorageSync('location').latitude,
        longitude: wx.getStorageSync('location').longitude
      },

      success(res) {
        that.setData({
          dataList: res.data.result
        })
        console.log(res.data)
      }
    })
  },
  methods: {

    /**
     * 跳转门店详情页
     */
    _onTargetDetail(e) {
      wx.navigateTo({
        url: '/pages/shop/detail/index?shop_id=' + e.detail.target.dataset.id,
      });
    },
  },
  mendian(e) {
    this.setData({
      mendian1: e.detail.value
    })
  },
  lianxiren(e) {

    this.setData({
      lianxiren1: e.detail.value
    })
  },
  shijian(e) {

    this.setData({
      shijian1: e.detail.value
    })
  },
  dianhua(e) {

    this.setData({
      dianhua1: e.detail.value
    })
  },
  jieshao(e) {

    this.setData({
      jieshao1: e.detail.value
    })
  },



  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },

  tijiao() {
    console.log(444444)
    var that = this
    console.log(that.data.mendian1)
    if (that.data.mendian1 == '') {
      console.log(66666)
      wx.showToast({
        title: '门店名称不可为空',
        icon: 'error',
        duration: 2000
      })
      return false;
    }
    if (that.data.shijian1 == '') {
      wx.showToast({
        title: '营业时间不可为空',
        icon: 'error',
        duration: 2000
      })
      return false;
    }
    if (that.data.lianxiren1 == '') {
      wx.showToast({
        title: '联系人不可为空',
        icon: 'error',
        duration: 2000
      })
      return false;
    }
    if (that.data.dianhua1 == '') {
      wx.showToast({
        title: '电话不可为空',
        icon: 'error',
        duration: 2000
      })
      return false;
    }
    if (that.data.address == '') {
      wx.showToast({
        title: '地址不可以为空',
        icon: 'error',
        duration: 2000
      })
      return false;
    }
    if (!wx.getStorageSync('logo_image_id')) {
      wx.showToast({
        title: 'logo不可为空',
        icon: 'error',
        duration: 2000
      })
      return false;
    }
    if (that.data.jieshao1 == '') {
      wx.showToast({
        title: '介绍不可为空',
        icon: 'error',
        duration: 2000
      })
      return false;
    }
    wx.request({
      url: 'https://weicheng.6sjs.com/index.php?s=/api/manba/mendian', //仅为示例，并非真实的接口地址
      data: {
        shop_name: that.data.mendian1,
        logo_image_id: wx.getStorageSync('logo_image_id'),
        linkman: that.data.lianxiren1,
        phone: that.data.dianhua1,
        shop_hours: that.data.shijian1,
        address: that.data.address,
        longitude: that.data.longitude,
        latitude: that.data.latitude,
        summary: that.data.jieshao1,
        region0: that.data.region[0],
        region1: that.data.region[1],
        region2: that.data.region[2]
      },

      success(res) {
        console.log(res.data)
        if (res.data.code == 200) {
          wx.showToast({
            title: '提交成功等待审核',
            icon: 'success',
            duration: 2000
          })
          setTimeout(function () {
            wx.switchTab({
              url: '/pages/index/index',
            })
          }, 2000);

        }
      }
    })

  },
  inDelImg(e) {
    this.setData({
      uploadImgData: e.detail.item
    })
    console.log(e.detail)
  },
  inUploadImg(e) {
    this.setData({
      uploadImgData: e.detail
    })
    wx.uploadFile({
      url: 'https://weicheng.6sjs.com/index.php?s=/api/upload/image', //仅为示例，非真实的接口地址
      filePath: e.detail.src,
      name: 'iFile',
      formData: {
        'wxapp_id': '10001',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        var data = res.data
        console.log(res.data)
        console.log(JSON.parse(res.data))
        wx.setStorageSync('file_id', JSON.parse(res.data).data.file_id)
        //do something
      }
    })
    console.log(e.detail)
    console.log(wx.getStorageSync('file_id'))
  },
  map() {
    var _that = this
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        const speed = res.speed
        const accuracy = res.accuracy
        console.log(res)
      }
    })
    wx.chooseLocation({
      success(res) {
        console.log(res)
        _that.setData({
          address: res.address,
          longitude: res.longitude,
          latitude: res.latitude,
        })
      }
    })
  },
  inDelImg1(e) {
    this.setData({
      uploadImgData1: e.detail.item
    })
    console.log(e.detail)
  },
  inUploadImg1(e) {
    this.setData({
      uploadImgData1: e.detail
    })
    console.log(e.detail)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})